import React, { Component } from 'react';

// import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import { Router, Route, Switch } from 'react-router-dom';
import { getToken } from 'utils/storage';
import Layout from './pages/Layout';
import Login from './pages/Login';
import history from './utils/history';
import TagImg from './pages/Tagimg';
// 封装路由组件
export default class App extends Component {
  render() {
    return (
      <Router history={history}>
        {/* // <Router> */}
        <Switch>
          <Route path="/login" component={Login}></Route>
          <Route path="/tagImg" component={TagImg}></Route>
          {/* 把 props重新赋值回去*/}
          <AuthRoute path="/" component={Layout}></AuthRoute>
        </Switch>
      </Router>
    );
  }
}

// 独立的组件
function AuthRoute(props) {
  const { path, component: Comy, ...rest } = props;

  return (
    <Route
      path={path}
      render={(props) => {
        if (!!getToken()) {
          return <Comy {...rest}></Comy>;
        }
        props.history.push('/login');
        // return <Login></Login>;
      }}
    ></Route>
  );
}
